<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/common/common_css :: commonHeader('系统权限列表')">
</head>
<link th:href="@{/js/admin.ui/lib/jquery.treetable/jquery.treetable.css}" rel="stylesheet" type="text/css" />
<link th:href="@{/js/admin.ui/lib/kkpager/kkpager_blue.css}" rel="stylesheet" type="text/css" />
<body>
<nav class="breadcrumb">
    <i class="Hui-iconfont">&#xe62e;</i> 系统管理
    <span class="c-gray en">&gt;</span> 权限管理
    <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" >
        <i class="Hui-iconfont">&#xe68f;</i>
    </a>
</nav>
<div class="page-container">
    <div class="cl pd-5 bg-1 bk-gray">
				<span class="l">
					<a href="javascript:deleteBatchAuthority();" class="btn btn-danger radius">
						<i class="Hui-iconfont">&#xe6e2;</i> 批量删除权限
					</a>
					<a href="javascript:gotoAddRootPage();" class="btn btn-success radius">
						<i class="Hui-iconfont">&#xe600;</i> 添加根权限
					</a>
					<a href="javascript:exportAuthorityInfo();" class="btn btn-primary radius">
						<i class="Hui-iconfont">&#xe644;</i> 导出权限信息
					</a>
				</span>
        <span class="r" id="pageSizeSpan">
					页面显示
					<select id="pageSizeSelect" style="height: 25px; font-size: 14px;" onchange="javascript:reloadPageSizeData();">
						<option value="10">10</option>
						<option value="25">25</option>
						<option value="50">50</option>
						<option value="100">100</option>
					</select>
					条数据
					,
					共<span id="totalRecordsSpan"></span>条数据
				</span>
    </div>
    <div class="mt-20">
        <table class="table table-border table-bordered table-bg" id="treeTable">
            <thead>
            <tr>
                <th style="text-align: center;">#</th>
                <th>权限编码</th>
                <th>权限级别</th>
                <th>权限名称</th>
                <th>权限标识</th>
                <th>权限地址</th>
                <th>权限说明</th>
                <th style="text-align: center;">操作</th>
            </tr>
            </thead>
            <tbody id="treeTableBody">
            </tbody>
            <tfoot>
            <tr>
                <td colspan="8">
                    <div id="kkpager"></div>
                </td>
            </tr>
            </tfoot>
        </table>
    </div>
    <form id="exportForm" action="export" method="post"></form>
</div>
<div th:include="admin/common/common_js :: onLoadJS"></div>
<script type="text/javascript" th:src="@{/js/admin.ui/lib/jquery.treetable/jquery.treetable.js}"></script>
<script type="text/javascript" th:src="@{/js/admin.ui/lib/kkpager/kkpager.min.js}"></script>
<script type="text/javascript">
    // 初始化treeTable属性
    var option = {
        expandable:true,
        column:1,
        clickableNodeNames:true
    };
    $("#treeTable tbody").on("mousedown", "tr", function() {
        $(".selected").not(this).removeClass("selected");
        $(this).toggleClass("selected");
    });

    // 加载treeTable数据
    function selectTreeTableData(pageNo, pageSize) {
        send(
            '/pestdiagnosis/sys/authority/treeTable',
            {'pageNo':pageNo, 'pageSize':pageSize},
            function (data) {
                if(data.result == 0) {
                    var treeTableData = '';
                    $.each(data.data, function (index, val) {
                        if(val.authorityLevel == 1) {
                            treeTableData +=
                                '<tr data-tt-id="' + val.authorityId + '">'
                                + ' <td style="text-align: center;"><input type="checkbox" value="' + val.authorityId + '" name="authorityIdCheckBox" onclick="javascript:dynamicCheck(this);" /></td>'
                                + '	<td>' + val.authorityCode + '</td>'
                                + '	<td>' + val.authorityLevel + '</td>'
                                + '	<td>' + val.authorityName + '</td>'
                                + '	<td>' + val.authorityKey + '</td>'
                                + '	<td>' + val.authorityUrl + '</td>'
                                + ' <td>' + shiro.util.formatTableData('—', val.authorityDesc) + '</td>'
                                + ' <td style="text-align: center;">'
                                + '		<a title="添加" href="javascript:;" onclick="javascript:gotoAddSubPage(' + val.authorityId + ',\'' + val.authorityName + '\');" style="text-decoration:none">'
                                + '			<i class="Hui-iconfont">&#xe600;</i>'
                                + '		</a>'
                                + '		<a title="编辑" href="javascript:;" onclick="javascript:gotoEditPage(' + val.authorityId + ',\'' + val.authorityName + '\');" style="text-decoration:none">'
                                + '			<i class="Hui-iconfont">&#xe6df;</i>'
                                + '		</a>'
                                + '		<a title="删除" href="javascript:;" onclick="javascript:deleteAuthority(' + val.authorityId + ',' + val.isParent + ')" class="ml-5" style="text-decoration:none">'
                                + '			<i class="Hui-iconfont">&#xe6e2;</i>'
                                + '		</a>'
                                + '	</td>'
                                + '</tr>';
                            if(val.isParent == 1 && val.subList) {
                                treeTableData += recursionSubAuthority(val.subList, val.authorityId);
                            }
                        }
                    });
                    $("#treeTableBody").empty();
                    $("#treeTableBody").append(treeTableData);
                    $("#treeTable").treetable(option, true);
                    var kkpagerObj = eval('(' + data.message + ')');
                    createPager(kkpagerObj.pno, kkpagerObj.total, kkpagerObj.totalRecords);
                    $('#totalRecordsSpan').text(kkpagerObj.totalRecords);
                }
            },
            function (res) {

            }
        );
    }

    /**
     * 递归添加权限子集
     * @param resultList 结果List
     * @param authorityPic 父级权限Id
     */
    function recursionSubAuthority(subList, authorityPid) {
        var result = '';
        $.each(subList, function (index, val) {
            result +=
                '<tr data-tt-id="' + val.authorityId + '" data-tt-parent-id="' + authorityPid + '">'
                + ' <td style="text-align: center;"><input type="checkbox" value="' + val.authorityId + '" name="authorityIdCheckBox" onclick="javascript:dynamicCheck(this);" /></td>'
                + '	<td>' + val.authorityCode + '</td>'
                + '	<td>' + val.authorityLevel + '</td>'
                + '	<td>' + val.authorityName + '</td>'
                + '	<td>' + val.authorityKey + '</td>'
                + '	<td>' + val.authorityUrl + '</td>'
                + ' <td>' + shiro.util.formatTableData('—', val.authorityDesc) + '</td>'
                + ' <td style="text-align: center;">'
                + '		<a title="添加" href="javascript:;" onclick="javascript:gotoAddSubPage(' + val.authorityId + ',\'' + val.authorityName + '\');" style="text-decoration:none">'
                + '			<i class="Hui-iconfont">&#xe600;</i>'
                + '		</a>'
                + '		<a title="编辑" href="javascript:;" onclick="javascript:gotoEditPage(' + val.authorityId + ',\'' + val.authorityName + '\');" style="text-decoration:none">'
                + '			<i class="Hui-iconfont">&#xe6df;</i>'
                + '		</a>'
                + '		<a title="删除" href="javascript:;" onclick="javascript:deleteAuthority(' + val.authorityId + ',' + val.isParent + ')" class="ml-5" style="text-decoration:none">'
                + '			<i class="Hui-iconfont">&#xe6e2;</i>'
                + '		</a>'
                + '	</td>'
                + '</tr>';
            if(val.isParent == 1 && val.subList) {
                result += recursionSubAuthority(val.subList, val.authorityId);
            }
        });
        return result;
    }

    // 动态勾选权限信息
    function dynamicCheck(obj) {
        var isChecked = $(obj).prop('checked');
        var subArray = new Array();
        recursionSub(obj, subArray);
        for(var i = 0; i < subArray.length; i++) {
            var sid = subArray[i];
            $('#treeTableBody').find("tr").each(function(){
                var authorityId = $(this).attr('data-tt-id');
                if(sid == authorityId) {
                    var tdArr = $(this).children();
                    tdArr.eq(0).find('input').prop('checked', isChecked);
                }
            });
        }
    }

    // 递归查找子权限
    function recursionSub(obj, subArray) {
        var authorityId = $(obj).parent().parent().attr('data-tt-id');
        $('#treeTableBody').find("tr").each(function(){
            var subAuthorityId = $(this).attr('data-tt-id');
            var subAuthorityPid = $(this).attr('data-tt-parent-id');
            if(subAuthorityPid == authorityId) {
                subArray.push(subAuthorityId);
                var tdArr = $(this).children();
                recursionSub(tdArr.eq(0).find('input'), subArray);
            }
        });
    }

    // 生成分页数据
    function createPager(pno, total, totalRecords) {
        //生成分页
        kkpager.generPageHtml({
            pno : pno,
            //总页码
            total : total,
            //总数据条数
            totalRecords : totalRecords,
            mode : 'click',
            click : function(n){
                selectTreeTableData(n, $('#pageSizeSelect').val());
                this.selectPage(n);
                return false;
            }
        }, true);
    }

    // 切换每页显示数据条数重新加载数据
    function reloadPageSizeData() {
        selectTreeTableData(1, $('#pageSizeSelect').val());
    }

    // 页面加载执行
    jQuery(function ($) {
        selectTreeTableData(1, $('#pageSizeSelect').val());
    })

    // 进入添加根权限页面
    function gotoAddRootPage() {
        layer_show('添加根权限', '/pestdiagnosis/sys/authority/toAddRoot',800,400);
    }

    // 进入添加子权限页面
    function gotoAddSubPage(authorityId, authorityName) {
        layer_show('添加【' + authorityName + '】的子权限', '/pestdiagnosis/sys/authority/toAddSub?authorityPid=' + authorityId,800,400);
    }

    // 进入编辑权限页面
    function gotoEditPage(authorityId, authorityName) {
        layer_show('编辑【' + authorityName + '】权限', '/pestdiagnosis/sys/authority/toEdit?authorityId=' + authorityId,800,400);
    }

    // 删除权限信息
    function deleteAuthority(authorityId, isParent) {
        var hitMessage = '确认要删除该权限？';
        if(isParent == 1) {
            hitMessage = '【系统提示】您想要删除的权限含有子权限，系统会同步删除所有子权限，请谨慎操作，确认删除？';
        }
        layer.confirm(hitMessage, {title:'系统消息'},function(index){
            send(
                '/pestdiagnosis/sys/authority/delete',
                {'authorityId' : authorityId},
                function (data) {
                    if(data.result == 0) {
                        layer.msg(data.message, {icon: 1,time: 1500}, function() {
                            window.location.href = "/pestdiagnosis/sys/authority/list";
                        });
                    } else {
                        layer.msg(data.message, {icon: 5,time: 1500});
                    }
                },
                function (res) {

                }
            );

        });
    }

    // 批量删除权限信息
    function deleteBatchAuthority() {
        var authorityIdArr = shiro.form.getCheckBoxSelectValues('authorityIdCheckBox');
        if(authorityIdArr.length == 0) {
            layer.msg('请选择要操作的数据行', {icon: 5,time: 1500});
        } else {
            var hitMessage = '【系统提示】批量删除操作将会删除一个或多个您选择的权限，请谨慎操作，确认删除？';
            layer.confirm(hitMessage, {title:'系统消息'},function(index){
                send(
                    '/pestdiagnosis/sys/authority/batchDelete',
                    {'authorityIds' : authorityIdArr.join(',')},
                    function (data) {
                        if(data.result == 0) {
                            layer.msg(data.message, {icon: 1,time: 1500}, function() {
                                window.location.href = "/pestdiagnosis/sys/authority/list";
                            });
                        } else {
                            layer.msg(data.message, {icon: 5,time: 1500});
                        }
                    },
                    function (res) {

                    }
                );

            });
        }
    }

    // 导出权限信息
    function exportAuthorityInfo() {
        var hitMessage = '您确定要导出权限信息到Excel文件？';
        layer.confirm(hitMessage, {title:'系统消息'},function(index){
            $('#exportForm').submit();
            layer.msg('数据导出中...', {time: 1000});
        });
    }
</script>
</body>
</html>